package edu.sctu.practice.class02.hujie.cgs.assign07.one;


import javax.sound.sampled.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by hp on 2015/12/18.
 */
public class StudyFrame extends Frame implements ItemListener, ActionListener, Runnable {
    ChineseCharacters chinese;
    Choice choice;
    Button getCharaters, voiceCharaters;
    Label showCharaters;
    StringBuffer trainedChinese = null;
    Clip clip = null;
    Thread voiceThread;
    int k = 0;
    Panel pCenter;
    CardLayout mycard;
    TextArea textHelp;
    MenuBar menubar;
    Menu menu;
    MenuItem help;

    public StudyFrame() {
        chinese = new ChineseCharacters();
        choice = new Choice();
        choice.add("training1.txt");
        choice.add("training2.txt");
        choice.add("training3.txt");
        showCharaters = new Label("", Label.CENTER);
        showCharaters.setFont(new Font("宋体", Font.BOLD, 72));
        showCharaters.setBackground(Color.green);
        getCharaters = new Button("下一个汉字");
        voiceCharaters = new Button("发音");
        voiceThread = new Thread(this);
        choice.addItemListener(this);
        voiceCharaters.addActionListener(this);
        getCharaters.addActionListener(this);
        Panel pNorth = new Panel();
        pNorth.add(new Label("选择一个汉字字符组成的文件"));
        pNorth.add(choice);
        add(pNorth, BorderLayout.NORTH);
        Panel pSouth = new Panel();
        pSouth.add(getCharaters);
        pSouth.add(voiceCharaters);
        add(pNorth, BorderLayout.SOUTH);
        pCenter = new Panel();
        mycard = new CardLayout();
        pCenter.setLayout(mycard);
        textHelp = new TextArea();
        pCenter.add("hanzi", showCharaters);
        pCenter.add("help", textHelp);
        add(pCenter, BorderLayout.CENTER);
        menubar = new MenuBar();
        menu = new Menu("帮助");
        help = new MenuItem("关于学汉字");
        help.addActionListener(this);
        menu.add(help);
        menubar.add(menu);
        setMenuBar(menubar);
        setSize(350, 220);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        validate();
    }

    public void itemStateChanged(ItemEvent e) {
        String fileName = choice.getSelectedItem();
        File file = new File(fileName);
        trainedChinese = chinese.getChinesechasracters(file);
        k = 0;
        mycard.show(pCenter, "hanzi");
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == getCharaters) {
            if (trainedChinese != null) {
                char c = trainedChinese.charAt(k);
                k++;
                if (k >= trainedChinese.length())
                    k = 0;
                showCharaters.setText("" + c);
            } else {
                showCharaters.setText("请选择一个汉字字符文件");
            }
        }
        if (e.getSource() == voiceCharaters) {
            if (!(voiceThread.isAlive())) {
                voiceThread = new Thread(this);
            }
            try {
                voiceThread.start();
            } catch (Exception exp) {
            }
        }
        if (e.getSource() == help) {
            mycard.show(pCenter, "help");
            try {
                File helpFile = new File("help.txt");
                FileReader inOne = new FileReader(helpFile);
                BufferedReader inTwo = new BufferedReader(inOne);
                String s = null;
                while ((s = inTwo.readLine()) != null) {
                    textHelp.append(s + "\n");
                }
                inOne.close();
                inTwo.close();
            } catch (IOException exp) {
            }
        }
    }

    public void run() {
        voiceCharaters.setEnabled(false);
        try {
            if (clip != null) {
                clip.close();
            }
            clip = AudioSystem.getClip();
            File voiceFile = new File(showCharaters.getText().trim() + ".wav");
            clip.open(AudioSystem.getAudioInputStream(voiceFile));
        } catch (Exception e) {
        }
        clip.start();
        voiceCharaters.setEnabled(true);
    }
}
